<?php

namespace app\admin\controller;

use app\admin\BaseController;
use app\model\AdminModel;

class Admin extends BaseController
{
    public function query()
    {
        $ps = password_hash('123456',PASSWORD_DEFAULT);
        $params = $this->request->param();

        $userinfo = $this->request->userInfo;


//        $list = AdminModel::where('pid',$userinfo->adminId)->paginate([
//            'list_rows' => $params['pageSize'],
//            'page' => $params['pageNo']
//        ]);
        $list = AdminModel::where('pid',$userinfo->adminId)->select()->toArray();

//        AdminModel::where('pid',)
        return show_serve([
            'data' => $list,
            'current_page' => 1,
            'total'=> 1
        ]);
    }
    public function add()
    {
        $params = $this->request->param();
        $userinfo = $this->request->userInfo;

        $adminModel = AdminModel::where('username',$params['username'])->find();
        if ($adminModel !== null) {
            show_serve(message: '账号已存在',code: 1);
        }

        AdminModel::create([
            'username' => $params['username'],
            'password' => password_hash($params['password'],PASSWORD_DEFAULT),
            'status' => $params['status'] ? 0 : 1,
            'password_origin' => $params['password'],
            'pid' => $userinfo->adminId
        ]);

        return show_serve();


    }

    public function status()
    {
        $params = $this->request->param();
        $adminModel = AdminModel::find($params['id']);
        $adminModel->status = $adminModel->status === 0 ? 1: 0;
        $adminModel->save();
        return show_serve();

    }

    public function del()
    {
        $params = $this->request->param();
        AdminModel::destroy($params['id']);
        return show_serve();
    }

    public function changePassword()
    {
        $params = $this->request->param();
        $adminModel = AdminModel::find($params['id']);
        if (password_verify($params['password'], $adminModel->password) === false) {
            return show_serve(message:'密码错误',code: 1);
        }
        if ($params['new_password'] !== $params['confirm']) {
            return show_serve(message:'两次密码不相同',code: 1);
        }
        $adminModel->password = password_hash($params['new_password'],PASSWORD_DEFAULT);
        $adminModel->password_origin = $params['new_password'];
        $adminModel->save();
        return show_serve();
    }


}